package com.benjaminwan.read.free.utils

import android.widget.CheckBox
import android.widget.Switch
import androidx.core.content.ContextCompat
import com.benjaminwan.read.free.R
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

var SuperTextView.enable: Boolean
    get() = this.isEnabled
    set(value) {
        this.isEnabled = value
        this.isClickable = value
        //this.setSwitchEnable(value)
        //this.setCheckBoxEnable(value)
        if (value) {
            this.leftTextView.setTextColor(ContextCompat.getColor(this.context, R.color.black))
            this.rightTextView.setTextColor(
                ContextCompat.getColor(
                    this.context,
                    R.color.colorPrimary
                )
            )
        } else {
            this.leftTextView.setTextColor(
                ContextCompat.getColor(
                    this.context,
                    R.color.material_grey_600
                )
            )
            this.rightTextView.setTextColor(
                ContextCompat.getColor(
                    this.context,
                    R.color.material_grey_600
                )
            )
        }
    }


fun SuperTextView.setSwitchEnable(isEnable: Boolean) {
    try {
        val field = this.javaClass.getDeclaredField("mRightSwitch")
        field.isAccessible = true
        val sw = field.get(this) ?: return
        if (sw is Switch) {
            sw.isClickable = isEnable
            sw.isEnabled = isEnable
            field.set(this, sw)
        }
    } catch (e: Exception) {
        //e.printStackTrace()
    }
}

fun SuperTextView.setCheckBoxEnable(isEnable: Boolean) {
    try {
        val field = this.javaClass.getDeclaredField("mRightCheckBox")
        field.isAccessible = true
        val cb = field.get(this) ?: return
        if (cb is CheckBox) {
            cb.isClickable = isEnable
            cb.isEnabled = isEnable
            field.set(this, cb)
        }
    } catch (e: Exception) {
    }
}
